var APPID = '20180821000197129';
var SECRET_KEY = '0zyujmb9EoksrFkC2sKy';

import md5 from 'js-md5';
import $ from 'jquery';

var Translate = function(str, from, to, callback) {
	if('' == str) {
		callback(str);
		return;
	}
	var salt = (new Date).getTime();
	console.log(salt);

	var str1 = APPID + str + salt + SECRET_KEY;
	console.log(str1);
	var sign = md5(str1);	
	console.log(sign);
	$.ajax({
		url: '/baidu/translate',
		type: 'POST',
		dataType: 'json',
		data: {
			q: str,
			appid: APPID,
			salt: salt,
			from: from,
			to: to,
			sign: sign
		},
		success: function(data) {
			console.log(data);
			var trans_result = data.trans_result;
			var result = '';
			for(var i = 0; i < trans_result.length; i++) {
				var tran = trans_result[i];
				result += tran.dst;
			}
			callback(result);
		}
	});
};

export default Translate;